{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding: utf-8\n",
    "import uuid\n",
    "import time\n",
    "import requests\n",
    "from auth_util import gen_sign_headers\n",
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 请替换APP_ID、APP_KEY\n",
    "APP_ID = '3037479757'\n",
    "APP_KEY = 'RcMRkqNoBOnzqyAf'\n",
    "URI = '/vivogpt/completions'\n",
    "DOMAIN = 'api-ai.vivo.com.cn'\n",
    "METHOD = 'POST'\n",
    "\n",
    "\n",
    "def sync_vivogpt(prompt, temperature=0.9):\n",
    "    params = {\n",
    "        'requestId': str(uuid.uuid4())\n",
    "    }\n",
    "    print('requestId:', params['requestId'])\n",
    "\n",
    "    data = {\n",
    "        'prompt': prompt,\n",
    "        'model': 'vivo-BlueLM-TB',\n",
    "        'sessionId': str(uuid.uuid4()),\n",
    "        'extra': {\n",
    "            'temperature': temperature\n",
    "        }\n",
    "    }\n",
    "    headers = gen_sign_headers(APP_ID, APP_KEY, METHOD, URI, params)\n",
    "    headers['Content-Type'] = 'application/json'\n",
    "\n",
    "    start_time = time.time()\n",
    "    url = 'https://{}{}'.format(DOMAIN, URI)\n",
    "    response = requests.post(url, json=data, headers=headers, params=params, stream=True)\n",
    "    content = ''\n",
    "    if response.status_code == 200:\n",
    "        res_obj = response.json()\n",
    "        print(f'response:{res_obj}')\n",
    "        if res_obj['code'] == 0 and res_obj.get('data'):\n",
    "            content = res_obj['data']['content']\n",
    "            # print(f'final content:\\n{content}')\n",
    "    else:\n",
    "        print(response.status_code, response.text)\n",
    "    end_time = time.time()\n",
    "    timecost = end_time - start_time\n",
    "    print('请求耗时: %.2f秒' % timecost)\n",
    "    return content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "prompt = \"\"\"给我讲一个三国演义中你最喜欢的故事\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "requestId: 96f70dc4-0cc3-424c-8f4a-e3e1532d94ea\n",
      "response:{'code': 0, 'data': {'sessionId': '5f0fbbbe-1af6-4c0f-82ce-c33872d3e162', 'requestId': '13e7433d-e6d9-471c-91da-a89138d4ea44', 'content': '我最喜欢的三国演义故事是赤壁之战。这场战役是中国历史上最著名的战役之一，也是三国时期最具代表性的战役。在赤壁之战中，曹操率领的大军号称百万，而孙刘联军只有不到十万人。但是，孙刘联军的指挥官周瑜和诸葛亮利用风助火，火烧连营，最终成功击败了曹操的大军。这场战役不仅是中国历史上最经典的战争之一，也是一场智慧和勇气的较量，让人难以忘怀。', 'image': None, 'functionCall': None, 'toolCall': None, 'toolCalls': None, 'contentList': None, 'searchInfo': None, 'usage': {'promptTokens': None, 'completionTokens': None, 'totalTokens': None, 'duration': None}, 'provider': 'vivo', 'model': 'vivo-BlueLM-TB', 'finishReason': 'STOP'}, 'msg': 'done.'}\n",
      "请求耗时: 4.66秒\n"
     ]
    }
   ],
   "source": [
    "# chat\n",
    "res = sync_vivogpt(prompt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "我最喜欢的三国演义故事是赤壁之战。这场战役是中国历史上最著名的战役之一，也是三国时期最具代表性的战役。在赤壁之战中，曹操率领的大军号称百万，而孙刘联军只有不到十万人。但是，孙刘联军的指挥官周瑜和诸葛亮利用风助火，火烧连营，最终成功击败了曹操的大军。这场战役不仅是中国历史上最经典的战争之一，也是一场智慧和勇气的较量，让人难以忘怀。\n"
     ]
    }
   ],
   "source": [
    "print(res)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llama-factory",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
